package com.ruiysoft.controller.sms;

import com.ruiysoft.controller.sms.api.GreetingApi;
import com.ruiysoft.controller.sms.dto.ClerkDto;
import com.ruiysoft.controller.sms.dto.GreetingDto;
import com.ruiysoft.controller.sms.service.GreetingApiService;
import com.ruiysoft.controller.sys.api.service.SysUserApiService;
import com.ruiysoft.entity.sys.SysMicroShop;
import com.ruiysoft.entity.sys.SysShopPersonnel;
import com.ruiysoft.entity.sys.SysUser;
import com.ruiysoft.pub.core.Result;
import com.ruiysoft.pub.core.ResultGenerator;
import com.ruiysoft.security.entity.EntryUser;
import com.ruiysoft.service.sys.clent.LoginContextUtil;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created by 化增光 on 2019/2/22.
 */
@RestController
public class GreetingController implements GreetingApi {

    private static final Logger log = LoggerFactory.getLogger(GreetingController.class);

    @Resource
    SysUserApiService sysUserApiService;

//    @Autowired
//    private SimpMessagingTemplate messagingTemplate;

    @Resource
    GreetingApiService greetingApiService;

//    @MessageMapping("/message")
//    //@SendToUser(value = "/greeting",broadcast = false)
//    //客户端接收一对一消息的主题应该是“/user/” + 客户Id+分隔符+客服Id + “/greeting”
//    public void message(GreetingDto message) throws Exception{
//        System.out.println("进入后台！"+message.getContent());
//        //通过convertAndSendToUser 向用户发送信息,
//        // 第一个参数是接收消息的用户,第二个参数是浏览器订阅的地址,第三个参数是消息本身
//        messagingTemplate.convertAndSendToUser(message.getPrefix(),"/greeting", JSON.toJSONString(message));
//        //System.out.println("后台调用:"+message.getMessage());
//        //业务操作
//        //记录聊天信息
//        greetingApiService.addGreetings(message.getShopId(),message.getCustomer(),message.getCustomerService());
//        greetingApiService.addMessage(message.getShopId(),message.getCustomer(),message.getCustomerService(),message);
//    }

    @Override
    public Result<List<ClerkDto>> clerks() {
        List<ClerkDto> res =  new ArrayList<>();
        try {
            SysMicroShop microShop = LoginContextUtil.instance().getEntryMicroShop();

            SysUser sysUser = sysUserApiService.getShopkeeper(microShop.getId());
            SysShopPersonnel personnel = sysUserApiService.getSysShopPersonnel(sysUser.getPersonnelId());
            ClerkDto dto = new ClerkDto()
                    .setPersonnelId(personnel.getId())
                    .setNickName(personnel.getNickname())
                    .setHeadImgUrl(personnel.getHeadImgUrl());
            res.add(dto);
            return ResultGenerator.genSuccessResult(res);
        }catch (Exception e){
            log.error("clerks :",e);
            return ResultGenerator.genFailResult(e.getMessage());
        }
    }

    @Override
    public Result<List<ClerkDto>> shopGreetings() {
        List<ClerkDto> res = new ArrayList<>();
        try {
            EntryUser entryUser = LoginContextUtil.instance().getEntryUser();
            //当前登录用户
            SysMicroShop microShop = LoginContextUtil.instance().getEntryMicroShop();
            Set<Long> ids = greetingApiService.getGreetings(microShop.getId(),entryUser.getPersonnel().getId());
            if (null != ids && !ids.isEmpty()){

                for (Long id : ids) {
                    System.out.println("获取到 id: "+ id);
                    SysShopPersonnel personnel = sysUserApiService.getSysShopPersonnel(id);
                    ClerkDto dto = new ClerkDto()
                            .setPersonnelId(personnel.getId())
                            .setNickName(personnel.getNickname())
                            .setHeadImgUrl(personnel.getHeadImgUrl());
                    res.add(dto);
                }
            }
        }catch (Exception e){
            log.error("shopGreetings:",e);
           return ResultGenerator.genFailResult(e.getMessage());
        }
        return  ResultGenerator.genSuccessResult(res);
    }

    @Override
    public Result<List<GreetingDto>> messageRecord(
            @ApiParam(value = "分页开始页", required = true) @RequestParam(value = "pageStart") @Valid Integer pageStart,
            @ApiParam(value = "显示条数", required = true) @RequestParam(value = "pageSize") @Valid Integer pageSize,
            @ApiParam(value = "客户", required = true) @PathVariable("customer") Long customer,
            @ApiParam(value = "客服", required = true) @PathVariable("customerService") Long customerService) {
        //当前登录用户
        try {
            SysMicroShop microShop = LoginContextUtil.instance().getEntryMicroShop();
            List<GreetingDto> res = greetingApiService.getMessage(pageStart, pageSize, microShop.getId(),customer,customerService);
            return ResultGenerator.genSuccessResult(res);
        }catch (Exception e){
            log.error("messageRecord",e);
            return ResultGenerator.genFailResult(e.getMessage());
        }
    }


//
//    @Scheduled(fixedRate = 1000)
//    @SendTo("/topic/callback")
//    public GreetingDto callback() throws Exception {
//
//        return "callback";
//    }
}
